package com.dky.satell.controller;


import com.dky.satell.dto.ResultDto;
import com.dky.satell.entity.bo.RequestMappingBO;
import com.dky.satell.service.RequestMappingService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 数据采集-接口映射表 前端控制器
 * </p>
 *
 * @author fangyuan
 * @since 2021-06-02
 */
@RestController
@Slf4j
@RequestMapping("/gather/requestMapping")
@Api(description = "接口映射")
public class RequestMappingController {

    @Resource
    RequestMappingService requestMappingService;

//    @PostMapping(value = "/search",produces="application/json;charset=UTF-8")
//    @ApiOperation(value="条件查询")
//    public ResultDto search(@RequestBody RequestMappingBO request) {
//        try {
//            log.info("begin to /requestMapping/search");
//            Page<com.dky.satell.entity.RequestMapping> page = requestMappingService.search(request);
//            log.info("end /requestMapping/search");
//            return ResultDto.success("", page);
//        } catch (Exception e) {
//            log.error("error", e);
//            return ResultDto.fail("查询失败");
//        }
//    }

    @PostMapping(value = "/search",produces="application/json;charset=UTF-8")
    @ApiOperation(value="条件查询")
    public ResultDto search(@RequestBody Map<String, Object> maps) {
        try {
            log.info("begin to /requestMapping/search");
            Integer pageNum = (Integer) maps.get("pageNum");
            Integer pageSize = (Integer) maps.get("pageSize");
            String name = (String)maps.get("name");
            Page<com.dky.satell.entity.RequestMapping> search = requestMappingService.search(pageNum, pageSize, name);
            log.info("end /requestMapping/search");
            HashMap<String, Object> request = new HashMap<>();
            request.put("total", search.getTotal());
            request.put("pageNum", search.getPageNum());
            request.put("pages", search.getPages());
            request.put("pageSize", search.getPageSize());
            request.put("rows", search.getResult());
            return ResultDto.success("", request);
        } catch (Exception e) {
            log.error("error", e);
            return ResultDto.fail("查询失败");
        }
    }

    @GetMapping(value = "/get/{id}",produces="application/json;charset=UTF-8")
    @ApiOperation(value="根据id获取数据")
    public ResultDto get(@PathVariable("id") Integer id) {
        try {
            log.info("begin to /requestMapping/getById");
            com.dky.satell.entity.RequestMapping requestMapping = requestMappingService.getById(id);
            log.info("end /requestMapping/getById");
            return ResultDto.success("", requestMapping);
        } catch (Exception e) {
            log.error("error", e);
            return ResultDto.fail("请求失败");
        }
    }

    @PostMapping(value = "/insert",produces="application/json;charset=UTF-8")
    @ApiOperation(value="新增")
    public ResultDto insert(@RequestBody com.dky.satell.entity.RequestMapping requestMapping) {
        try {
            log.info("begin to /requestMapping/insert");
            ResultDto resultInfo= requestMappingService.insert(requestMapping);
            log.info("end /requestMapping/insert");
            return resultInfo;
        } catch (Exception e) {
            log.error("error", e);
            return ResultDto.fail("新增失败");
        }
    }

    @PostMapping(value = "/update",produces="application/json;charset=UTF-8")
    @ApiOperation(value="编辑")
    public ResultDto update(@RequestBody com.dky.satell.entity.RequestMapping requestMapping) {
        try {
            log.info("begin to /requestMapping/update");
            ResultDto resultInfo= requestMappingService.update(requestMapping);
            log.info("end /requestMapping/update");
            return resultInfo;
        } catch (Exception e) {
            log.error("error", e);
            return ResultDto.fail("编辑失败");
        }
    }

    @GetMapping(value = "/delete/{id}",produces="application/json;charset=UTF-8")
    @ApiOperation(value="删除")
    public ResultDto delete(@PathVariable("id") Integer id) {
        try {
            log.info("begin to /requestMapping/deleteById");
            requestMappingService.delete(id);
            return ResultDto.success();
        } catch (Exception e) {
            log.error("error", e);
            return ResultDto.fail("删除失败");
        }
    }

}

